<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\common\model;

use cores\BaseModel;

/**
 * 模型类：购物车
 * Class Cart
 * @package app\common\model
 */
class OrderSub extends BaseModel
{
    // 定义表名
    protected $name = 'order_sub';

    // 定义主键
    protected $pk = 'id';


    //三券支付信息写入订单子表
    public static function saveOrderSub($params)
    {
        if(empty($params)){
            return false;
        }

        $dk_money = !empty($params['dk_money']) ? $params['dk_money'] : '0.00';
        $pay_money = !empty($params['pay_money']) ? $params['pay_money'] : '0.00';
        $query = [];
        if(isset($params['order_id'])){
            $query =  (new static)->where(['order_id'=>$params['order_id']])->find();
        }
        $data['pay_type'] = isset($params['payType']) ? $params['payType'] : 20;//微信
        $data['bd_type'] = isset($params['bdType']) ? $params['bdType'] : 0;
        $data['deduction_value'] = $dk_money;
        $data['pay_money'] = $pay_money;
        if($query){
            return (new static)->where('order_id', $params['order_id'])
                ->where('state', 10)
                ->update($data);
        }else{
            $data['user_id'] = $params['user_id'];
            $data['store_id'] = $params['store_id'];
            $data['order_no'] = $params['order_no'];
            $data['order_id'] = $params['order_id'];
            $data['created_time'] = time();
            return (new static)->save($data);
        }
    }

    //通过订单号获取订单子表
    public static function getOrderSubBy($orderId)
    {
        return (new static)->where('order_id', '=', $orderId)
            ->find();
    }

    /**
     * 获取订单详情
     * @param string $orderNo
     * @param $pay_status 订单状态
     * @return OrderSub|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getOrderSubDetail(string $orderNo,$state)
    {
        return (new static)->where('order_no', '=', $orderNo)
            ->where('state', '=', $state)
            ->find();
    }

    /**
     * 获取商品列表
     * @param array $param 查询条件
     * @param int $listRows 分页数量
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param = [],$list_rows)
    {
        // 检索查询条件
        $filter = $this->getQueryFilter($param);
        // 执行查询
        $list = (new static)
            ->where($filter)
            ->order(['pay_time'=> 'DESC'])
            ->paginate($list_rows);
        //echo (new static)->getLastSql();
        return $list;
    }
    /**
     * 设置查询条件
     * @param array $param
     * @return array
     */
    private function getQueryFilter(array $param): array
    {
        // 设置默认的检索数据
        $params = $this->setQueryDefaultValue($param, [
            'user_id' => 0,
            'bd_type' => '',
            'state' => '',
            'pay_time' => []    // 起止时间
        ]);
        // 检索查询条件
        $filter = [];
        // 用户ID
        $params['user_id'] > 0 && $filter[] = ['user_id', '=', $params['user_id']];
        !empty($params['bd_type']) && $filter[] = ['bd_type', '=', $params['bd_type']];
        // 用户昵称
        !empty($params['state']) && $filter[] = ['state', 'in', [20,30]];

        // 起止时间
        if (!empty($params['betweenTime'])) {
            $times = $params['betweenTime'];
            $filter[] = ['pay_time', '>=', $times['start_time']];
            $filter[] = ['pay_time', '<', $times['end_time']];
        }
        return $filter;
    }

    //通过订单号更新订单子表
    public static function updateOrderSubBy($orderId,$params)
    {
        return (new static)->where('order_id', $orderId)
            ->update($params);
    }
}